package com.shop.controller;

import com.shop.bean.Order_address;
import com.shop.bean.Orders;
import com.shop.bean.Product;
import com.shop.bean.User;
import com.shop.service.IOrdersService;
import com.shop.util.LayData;
import com.shop.util.Page;
import com.shop.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("orders")
public class OrdersController {
    @Autowired
    IOrdersService orderService;
    @RequestMapping("add")
    @ResponseBody
    public LayData add(@RequestParam("id[]") List<Integer> id, @RequestParam("quantity[]") List<Integer> quantity, int addressid, HttpSession session){
        int userid= SYS.getLoginUserID(session);
        int orderid=orderService.addOrder(id,quantity,addressid,userid);
        return LayData.success(200,"订单创建成功",orderid);
    }

    @RequestMapping("payment")
    @ResponseBody
    public LayData payment(Integer id){
        String code=orderService.payment(id);
        System.out.println(code);
        return LayData.success(200,"",code);
    }

    @PostMapping("paymentsuccess")
    @ResponseBody
    public LayData paymentSuccess(@RequestParam HashMap map){
        System.out.println(map);
        orderService.paymentSuccess(map);
        return LayData.success(200,"");
    }

    @RequestMapping ("getorderstate")
    @ResponseBody
    public LayData getOrderState(Integer id){
        Orders order=orderService.getOrderByID(id);
        return LayData.success(200,"",order.getState());
    }

    @RequestMapping("mycheck")
    @ResponseBody
    public LayData myCheckout(HttpSession session){
        User loginuser = (User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        Integer userid = loginuser.getId();
        List<Orders> orders = orderService.getOrderByUserID(userid);
        return LayData.success(200,"",orders);
    }

    @RequestMapping("list")
    @ResponseBody
    public LayData getList(Page page, String text) {
        List<Orders> list=orderService.selectOrders(page, text);
        int count=orderService.getCount(text);
        return new LayData(count, list);
    }

    @RequestMapping("delete")
    @ResponseBody
    public int delete(int orderid){
        return orderService.deleteOrder(orderid);
    }

    @RequestMapping("getaddress")
    @ResponseBody
    public LayData getaddress(int orderid){
        Order_address address =orderService.getAddress(orderid);
        String description = orderService.getDescription(orderid);
        return LayData.success(200,description,address);
    }

    @RequestMapping("putOn")
    @ResponseBody
    public LayData putOn(int orderid){
        orderService.putOn(orderid);
        return LayData.success(200,"已发货");
    }

    @RequestMapping("end")
    @ResponseBody
    public LayData end(int  orderid){
        orderService.end(orderid);
        return LayData.success(200,"已结束订单");
    }

    @RequestMapping("get")
    @ResponseBody
    public LayData get(int  orderid){
        orderService.get(orderid);
        return LayData.success(200,"确认收货");
    }



}
